package com.yimi.appstore.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.yimi.appstore.R;
import com.yimi.appstore.fileexplorer.FileCategoryHelper.FileCategory;
import com.yimi.appstore.util.FileUtils;
import com.yimi.appstore.util.FileUtils.SDCardInfo;
import com.yimi.appstore.view.CategoryBar;

public class ResourceManagementActivity extends Activity {

	private Context context;
	private CategoryBar mCategoryBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.management_layout);
		initUi();
	}

	private void initUi() {
		findViewById(R.id.category_apps).setOnClickListener(new managementOnclickListener());
		findViewById(R.id.category_music_pic).setOnClickListener(new managementOnclickListener());
		findViewById(R.id.category_video).setOnClickListener(new managementOnclickListener());
		findViewById(R.id.category_text).setOnClickListener(new managementOnclickListener());
		findViewById(R.id.go_back_btn).setOnClickListener(new managementOnclickListener());
		mCategoryBar = (CategoryBar) findViewById(R.id.category_bar);
		mCategoryBar.addCategory(R.drawable.category_bar_apk);
		SDCardInfo sdCardInfo = FileUtils.getSDCardInfo();
		if (sdCardInfo != null) {
			mCategoryBar.setFullValue(sdCardInfo.total);
			setTextView(R.id.sd_card_capacity,
					getString(R.string.sd_card_size, FileUtils.convertStorage(sdCardInfo.total)));
			setTextView(R.id.sd_card_available,
					getString(R.string.sd_card_available, FileUtils.convertStorage(sdCardInfo.free)));
			setTextView(R.id.sd_card_used,
					getString(R.string.sd_card_used, FileUtils.convertStorage(sdCardInfo.total - sdCardInfo.free)));
			mCategoryBar.setCategoryValue(0, sdCardInfo.total - sdCardInfo.free);
		}
		if (mCategoryBar.getVisibility() == View.VISIBLE) {
			mCategoryBar.startAnimation();
		}
	}

	private class managementOnclickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.category_apps:
				ApkExplorerActivity.lanuchActivity(context);
				break;

			case R.id.category_music_pic:
				CategoryFileActivity.lanuchActivity(context, FileCategory.Music);
				break;

			case R.id.category_video:
				CategoryFileActivity.lanuchActivity(context, FileCategory.Video);
				break;
			case R.id.category_text:
				CategoryFileActivity.lanuchActivity(context, FileCategory.Picture);
				break;
			case R.id.go_back_btn:
				finish();
				break;
			}

		}

	}

	private void setTextView(int id, String t) {
		TextView text = (TextView) findViewById(id);
		text.setText(t);
	}

}
